package com.qk.management.controller;

import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.impl.FileCleanStatusService;
import com.qk.management.service.impl.OssFileCleanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/clean")
public class FileCleanController {
    
    @Autowired
    private OssFileCleanService ossFileCleanService;
    
    @Autowired
    private FileCleanStatusService fileCleanStatusService;
    
    /**
     * 立即执行文件清理任务
     */
    @LogAnno
    @PostMapping("/execute")
    public ResponseEntity<String> executeFileClean() {
        try {
            // 获取上次清理时间
            LocalDateTime lastCleanTime = fileCleanStatusService.getLastCleanTime();
            
            // 执行清理
            ossFileCleanService.cleanFiles(lastCleanTime);
            
            // 更新清理时间
            fileCleanStatusService.updateLastCleanTime(LocalDateTime.now());
            
            return ResponseEntity.ok("文件清理任务执行成功");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("文件清理任务执行失败: " + e.getMessage());
        }
    }
    
    /**
     * 执行全量文件清理（不考虑上次清理时间）
     */
    @LogAnno
    @PostMapping("/all")
    public ResponseEntity<String> executeFullFileClean() {
        try {
            // 执行全量清理
            ossFileCleanService.cleanFiles(null);
            
            // 更新清理时间
            fileCleanStatusService.updateLastCleanTime(LocalDateTime.now());
            
            return ResponseEntity.ok("全量文件清理任务执行成功");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("全量文件清理任务执行失败: " + e.getMessage());
        }
    }
}
